﻿using Identity_JWT.Dao.Model;
using Microsoft.AspNetCore.Identity;

namespace Identity_JWT.IdentityPolicy
{
    /// <summary>
    /// 自定义用户信息验证策略
    /// </summary>
    public class CustomUserPolicy:UserValidator<User>
    {
        public override async Task<IdentityResult> ValidateAsync(UserManager<User> manager, User user)
        {
            IdentityResult result= await base.ValidateAsync(manager, user);
            List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();
            if (user.UserName == "google")
            {
                errors.Add(new IdentityError
                {
                    Description = "不能使用google为用户名"
                });
            }
            if (!user!.Email!.ToLower().EndsWith("@163.com"))
            {
                errors.Add(new IdentityError
                {
                    Description = "只能使用163的邮箱"
                });
            }
            return errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray());
        }
    }
}
